<template>
  <div class="register-bg-new">
    <div class="register-center">
      <el-card shadow="hover" class="register-card-new">
        <div class="register-title-new">
          <el-icon size="32"><User /></el-icon>
          <span>用户注册</span>
        </div>
        <div class="register-link-new">
          已有账号？
          <el-link type="primary" @click="$emit('to-login')">去登录</el-link>
        </div>
        <el-form :model="form" status-icon>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username" placeholder="请输入用户名" clearable />
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password clearable />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="register" style="width: 100%;">注册</el-button>
          </el-form-item>
        </el-form>
        <el-alert v-if="msg" :title="msg" type="info" show-icon class="register-alert" />
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import axios from 'axios'
import { User } from '@element-plus/icons-vue'

const form = ref({ username: '', password: '' })
const msg = ref('')

const register = async () => {
  try {
    const res = await axios.post('http://localhost:8080/api/user/register', form.value)
    msg.value = res.data.message
  } catch (e) {
    msg.value = '注册失败'
  }
}
</script>

<style scoped>
.register-bg-new {
  min-height: 100vh;
  background: url('../../anheng.png') no-repeat center center;
  background-size: cover;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-center {
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
}

.register-card-new {
  border-radius: 18px;
  background: #2a2d32;
  box-shadow: 0 8px 32px 0 rgba(0,0,0,0.25);
  padding: 40px 32px 32px 32px;
  width: 400px;
  max-width: 90vw;
}

.register-title-new {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 28px;
  color: #fff;
  gap: 12px;
}

.register-title-new .el-icon {
  color: #c81e2b;
}

.register-link-new {
  text-align: right;
  margin-bottom: 12px;
  color: #aaa;
}

.register-link-new .el-link {
  color: #c81e2b;
}

.el-form {
  margin-top: 12px;
}

.el-form-item__label {
  color: #fff !important;
}

.el-input__wrapper {
  background: #33373e !important;
  border: 1px solid #4a4e58 !important;
  border-radius: 8px !important;
  box-shadow: none !important;
}

.el-input__wrapper:focus-within {
  border: 1px solid #c81e2b !important;
}

.el-input__inner {
  color: #fff !important;
  background: transparent !important;
}

.el-button {
  background: linear-gradient(90deg, #c81e2b 0%, #d9534f 100%);
  color: #fff;
  font-weight: bold;
  border: none;
  border-radius: 8px;
  box-shadow: none;
  transition: background 0.3s;
  padding: 18px 0;
  font-size: 16px;
}

.el-button:hover {
  opacity: 0.9;
}

.register-alert {
  margin-top: 20px;
  background: #23262f;
  border-radius: 6px;
  border: 1px solid #393e4f;
  color: #fff;
  box-shadow: none;
}
</style>